<template>
  <div class="login" id="login">
    <div class="card_small"></div>
    <div class="card">
      <div class="card_title">登录</div>
      <form target="iframe_display">
        <div class="card_username card_input">
          <input type="text" required v-model="loginFormData.username">
          <label for="" class="input_label">账号</label>
          <div class="input_bar"></div>
        </div>
        <div class="card_password card_input">
          <input type="password" required v-model="loginFormData.password">
          <label for="" class="input_label">密码</label>
          <div class="input_bar"></div>
        </div>
        <button class="card_go" @click="handleGoLogin">
          <span>登录</span>
        </button>
      </form>
      <!-- <div class="card_text" @click="handleForgetPassword">忘记密码？</div> -->
    </div>
    <div class="card register">
      <div class="card_title">注册
        <div class="card_title_close" @click="handleClickClose"></div>
      </div>
      <form target="iframe_display">
        <div class="card_username card_input">
          <input type="text" required v-model="registerFormData.nickname">
          <label for="" class="input_label">昵称</label>
          <div class="input_bar"></div>
        </div>
        <div class="card_password card_input">
          <input type="text" required v-model="registerFormData.username">
          <label for="" class="input_label">账号</label>
          <div class="input_bar"></div>
        </div>
        <div class="card_password card_input">
          <input type="password" required v-model="registerFormData.password">
          <label for="" class="input_label">密码</label>
          <div class="input_bar"></div>
        </div>
        <button class="card_go" @click="handleGoRegister">
          <span>注册</span>
        </button>
      </form>
      <div class="toggle" @click="handleClickToggle">
        <i class="iconfont icon-zhankai" v-if="iconVisible"></i>
      </div>
    </div>
    <iframe id="iframeDisplay" name="iframe_display" style="display: none;"></iframe>  
  </div>
</template>

<script lang="ts">
import { defineComponent, ref } from 'vue'
import { useRouter } from 'vue-router'
import { useStore } from 'vuex'
import { login, signup } from '../../api/user'
export default defineComponent({
  setup() {
    const store = useStore()
    const iconVisible = ref(true)
    const router = useRouter()
    const loginFormData = ref({
      username: '',
      password: ''
    })
    const registerFormData = ref({
      nickname: '',
      username: '',
      password: ''
    })
    // 登录
    const handleLogin = () => {
      login(loginFormData.value).then((res) => {
        if(typeof res.code === 'number' && res.code === 100) {
          store.dispatch('fetchBookList')
          store.state.isLogin = true
          localStorage.setItem('token', JSON.stringify(res.data.token))
          if(localStorage.getItem('book')) {
            store.commit('get_book',JSON.parse(localStorage.getItem('book') as string))
          } else {
            const bookList = store.state.bookList
            // eslint-disable-next-line @typescript-eslint/no-explicit-any
            bookList.forEach((book: any) => {
              if(book.defaultBook) {
                localStorage.setItem('book', JSON.stringify(book))
                store.commit('get_book',JSON.parse(localStorage.getItem('book') as string))
              }
            });
          }
          router.push('/')
        } else {
          alert('账号或密码错误')
        }
      })
    }
    // 注册
    const handleSignup = () => {
      signup(registerFormData.value).then(() => {
        handleClickClose()
      })
    }
    // 登录
    const handleGoLogin = () => {
      if(loginFormData.value.password && loginFormData.value.username) {
        handleLogin()
      }
    }
    // 切换到注册卡片
    const handleClickToggle = () => {
      document.getElementById('login')?.classList.add('active')
      iconVisible.value = false
    }
    // 切换到登陆卡片
    const handleClickClose = () => {
      document.getElementById('login')?.classList.remove('active')
      iconVisible.value = true
    }
    // 判断input是否为空
    const handleGoRegister = () => {
      if(registerFormData.value.username && registerFormData.value.password && registerFormData.value.nickname) {
          handleSignup()
      }
    }
    // 忘记密码
    const handleForgetPassword = () => {
      console.log('忘记密码');
    }
    return {
      loginFormData,
      registerFormData,
      iconVisible,
      handleGoLogin,
      handleClickToggle,
      handleClickClose,
      handleGoRegister,
      handleForgetPassword
    }
  }
})
</script>

<style scoped>
  .login {
    background-color: #e9e9e9;
    display: flex;
    align-content: center;
    justify-content: center;
    flex-direction: column;
    height: 100vh;
    font-family: '小星星';
    position: relative;
    overflow: hidden;
    background-image: url(http://cdn.omisheep.cn/upload/v1/img/323427056794730496.jpeg);
    background-size: cover;
  }
  .card_small {
    background-color: #f2f2f2;
    margin: 0 35px;
    height: 10px;
    border-radius: 6px 6px  0 0;
  }
  .card {
    background-color: #fafafa;
    margin: 0 20px;
    padding: 50px 0 30px;
    border-radius: 6px;
    position: relative;
    margin-bottom: 24%;
  }
  .card_title {
    border-left: 4px #F9C3C2 solid;
    height: 50px;
    line-height: 50px;
    color: #F9C3C2;
    font-size: 24px;
    font-weight: 600;
    padding-left: 50px;
    text-align: left;
  }
  .card_input {
    margin: 40px 0 0 50px;
    position: relative;
    text-align: start;
  }
  .card_input input {
    line-height: 50px;
    height: 50px;
    width: calc(100% - 50px);
    border: none;
    background-color: inherit;
    color: #F9C3C2;
  }
  .input_label {
    position: absolute;
    left: 0;
    font-size: 20px;
    line-height: 50px;
  }
  .input_bar {
    position: absolute;
    left: 0;
    width: calc(100% - 50px);
    height: 1px;
    background-color: #757575;
    bottom: 0;
  }
  .input_bar:before, .input_bar:after {
    content: "";
    position: absolute;
    background: #F9C3C2;
    width: 0;
    height: 2px;
    transition: 0.2s ease;
  }
  .input_bar:before {
    left: 50%;
  }
  .input_bar:after {
    right: 50%;
  }
  /* 点击input框时label的变化 */
  .card_input input:focus ~ label {
    color: #9d9d9d;
    transform: translate(-12%, -50%) scale(0.75);
  }
  .card_input input:valid ~ label {
  color: #9d9d9d;
  transform: translate(-12%, -50%) scale(0.75);
}
  .card_input input:focus ~ .input_bar:before, .card_input input:focus ~ .input_bar:after {
    width: 50%;
  }
  .card_go {
    margin: 30px 25% 0;
    width: 50%;
    height: 50px;
    line-height: 50px;
    text-align: center;
    font-size: 24px;
    border: #e3e3e3 2px solid;
    text-transform: uppercase;
    overflow: hidden;
    transition: 0.3s ease;
    position: relative;
  }
  .card_go span {
    position: relative;
    z-index: 1;
    color: #F9C3C2;
    transition: 0.3s ease;
  }
  .card_go:before {
    content: "";
    position: absolute;
    top: 50%;
    left: 50%;
    display: block;
    background: #F9C3C2;
    width: 30px;
    height: 30px;
    border-radius: 100%;
    margin: -15px 0 0 -15px;
    opacity: 0;
    transition: 0.3s ease;
  }
  .card .card_go:active {
    border-color: #F9C3C2;
  }
  .card .card_go:active span {
    color: #ffffff;
  }
  .card_go:active:before {
    opacity: 1;
    transform: scale(10);
  }
  .card_text {
    margin-top: 20px;
    color: #bababa;
  }
  .login_write {
    position: absolute;
    top: 40px;
    right: -5%;
    background-color: #F9C3C2;
    color: white;
    height: 80px;
    width: 80px;
    border-radius: 50%;
    line-height: 80px;
    font-size: 30px;
  }
  .card.register .card_title,
  .card.register .card_input,
  .card.register .card_go {
    left: 100px;
    opacity: 0;
    visibility: hidden;
  }
  .card.register {
    position: absolute;
    top: -200px;
    right: 0;
    z-index: 10;
    width: 80px;
    height: 80px;
    background: none;
    border-radius: 100%;
    box-shadow: none;
    padding: 0;
    transition: 0.3s ease;
    margin: 0 10px;
    /* Toggle */
    /* Title */
    /* Input */
    /* Button */
  }
  .card.register .toggle {
    position: relative;
    background: #F9C3C2;
    width: 80px;
    height: 80px;
    border-radius: 100%;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.12), 0 1px 2px rgba(0, 0, 0, 0.24);
    color: #ffffff;
    font-size: 30px;
    line-height: 80px;
    text-align: center;
    cursor: pointer;
    z-index: -1;
  }
  /* .card.register .toggle:before {
    content: "";
    color: #fff;
    display: inline-block;
    font-size: inherit;
    text-rendering: auto;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    transform: translate(0, 0);
  } */
  .login.active .card.register {
    top: 174px;
    width: calc(100% - 20px);
    height: auto;
    border-radius: 5px;
    padding: 60px 0 40px;
    overflow: hidden;
  }
  .login.active .card.register .toggle {
    position: absolute;
    top: 200px;
    left: 150px;
    box-shadow: none;
    transform: scale(10);
    transition: transform 0.3s ease;
  }
  .login.active .card.register .toggle:before {
    content: "";
  }
  .login.active .card.register .card_title,
  .login.active .card.register .card_input,
  .login.active .card.register .card_go {
    left: 0;
    opacity: 1;
    visibility: visible;
    transition: 0.3s ease;
  }
  .login.active .card.register .card_title {
    transition-delay: 0.3s;
  }
  .login.active .card.register .card_input {
    transition-delay: 0.4s;
  }
  .login.active .card.register .card_input:nth-child(2) {
    transition-delay: 0.5s;
  }
  .login.active .card.register .card_input:nth-child(3) {
    transition-delay: 0.6s;
  }
  .container.active .card.register .card_go {
    transition-delay: 0.7s;
  }

  .card.register .card_title {
    position: relative;
    border-color: #ffffff;
    color: #ffffff;
  }
  .card.register .card_title .card_title_close {
    cursor: pointer;
    position: absolute;
    top: 0;
    right: 42px;
    display: inline;
    color: #ffffff;
    font-size: 58px;
    font-weight: 400;
  }
  .card.register .card_input .input_bar {
    position: absolute;
    left: 0;
    width: calc(100% - 50px);
    height: 1px;
    background-color: #fff;
    bottom: 0;
  }
  .card.register .card_title .card_title_close::before {
    content: "×";
  }
  .card.register .card_input input {
    color: #ffffff;
  }
  .card.register .card_input input:focus ~ .input_label {
    color: #ffffff;
  }
  .card.register .card_input input:focus ~ .bar:before, .card.register .card_input input:focus ~ .bar:after {
    background: #ffffff;
  }
  .card.register .card_input input:valid ~ .input_label {
    color: #ffffff;
  }
  .card.register .card_input .input_label {
    color: rgba(255, 255, 255, 0.8);
  }
  .card.register .card_input .bar {
    background: rgba(255, 255, 255, 0.8);
  }
  .card.register .card_go button {
    width: 100%;
    background: #ffffff;
    border-color: #ffffff;
  }
  .card.register .card_go button span {
    color: #F9C3C2;
  }
  .card.register .card_go button:hover {
    background: rgba(255, 255, 255, 0.9);
  }
  .card.register .card_go button:active:before, .card.register .card_go button:focus:before {
    display: none;
  }
</style>